package com.ctg.itrdc.k8sconsole.service.impl;

import java.net.URL;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.ctg.itrdc.imageconsole.common.util.HttpRequestUtil;
import com.ctg.itrdc.imageconsole.data.dao.IClusterConfigDao;
import com.ctg.itrdc.imageconsole.data.dto.K8sRcDTO;
import com.ctg.itrdc.imageconsole.data.entity.ClusterConfig;
import com.ctg.itrdc.k8sconsole.service.IDeveloperService;
import com.ctg.itrdc.paspsdk.common.utils.file.PropertiesUtil;
import com.ctg.itrdc.paspsdk.common.utils.json.JSONUtils;
import com.ctg.itrdc.paspsdk.common.utils.type.StringUtils;

/**
 * 
 * @description: 
 * @version: v1.0.0 
 * @author: Piao.Wu
 * 
 * Modification History:
 * Date               Author      Version     Description
 * -----------------------------------------------------------------
 * 2016年9月23日 上午11:49:54     Piao.Wu       v1.0.0        create
 *
 */
@Component("developerService")
public class DeveloperServiceImpl implements IDeveloperService{

//	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Resource
    private IClusterConfigDao clusterConfigDao;
	
	@SuppressWarnings("static-access")
	@Override
	public String queryK8sResources(String clusterCode) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);
		
		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties","getClusterResrTotal.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl());
		// 创建URL连接
        URL url = new URL(urlString);
        HttpRequestUtil requestUtil = new HttpRequestUtil(url);
        WebTarget target = requestUtil.target;
        Response response = target.request().get();
        
        String nodesJsonStr =  requestUtil.getResponseJsonData(response);
        if (StringUtils.isEmpty(nodesJsonStr)) {
			return "request exception!";
		}
        Map<String, Object> map1 = JSONUtils.jsonToMap(nodesJsonStr);
        String getClusterResrTotalUrlStr = PropertiesUtil.getConfigValueCache("k8s.properties","getNodes.api");
        getClusterResrTotalUrlStr = MessageFormat.format(getClusterResrTotalUrlStr, clusterConfig.getMonitorUrl(), clusterCode);
		// 创建URL连接
        URL getClusterResrTotalUrl = new URL(getClusterResrTotalUrlStr);
        requestUtil = new HttpRequestUtil(getClusterResrTotalUrl);
        target = requestUtil.target;
        response = target.queryParam("perpage", 50).queryParam("page", 1).request().get();
        String totalJsonStr =  requestUtil.getResponseJsonData(response);
        if (StringUtils.isEmpty(nodesJsonStr)) {
			return "request exception!";
		}
        Map<String, Object> map2 = JSONUtils.jsonToMap(totalJsonStr);
        // 由于是2个接口，所以将2个接口返回的json组装在一起返回给  门户那边调用
        for(Map.Entry<String, Object> entry:map1.entrySet()){
        	map2.put(entry.getKey(), entry.getValue());
        }
        
        String jsonString = JSONUtils.objectToJson(map2, "0");
        
        return jsonString;
        
	}

	@Override
	public String queryRcs(K8sRcDTO rcDTO) throws Exception {
		Map<String, Object> paraMap =new HashMap<>();
		paraMap.put("rcMutliNamespace", rcDTO.getRcMutliNamespace());
		
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(rcDTO.getClusterCode());
		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties","getRcs.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), rcDTO.getClusterCode());
		// 创建URL连接
        URL url = new URL(urlString);
        HttpRequestUtil requestUtil = new HttpRequestUtil(url);
        WebTarget target = HttpRequestUtil.target;
        Response response = target.request().post(Entity.entity(paraMap,  MediaType.APPLICATION_JSON_TYPE));
        
        return requestUtil.getResponseJsonData(response);
	}

	
	

	
	
}
